package api

import (
	"git.supremind.info/product/box/vehicle/common"
	"git.supremind.info/product/box/vehicle/internal/dao"
	"git.supremind.info/product/box/vehicle/internal/middleware"
	"git.supremind.info/product/box/vehicle/internal/service"
	"git.supremind.info/product/box/vehicle/internal/wsserver"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 登陆接口 用户名和密码登陆
func Login(c *gin.Context) {
	username := c.Query("username")
	password := c.Query("password")
	user, err := dao.LoginCheck(username, password)
	if err == nil {
		service.GenerateToken(c, user)
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    -1,
			"message": err.Error(),
		})
	}
}

func LogOut(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code":    -1,
		"message": "验证失败",
	})
}

func RegisterUser(c *gin.Context) {
	param := c.PostForm("username")
	common.Logger.Infof("注册用户： %s", param)
}

// 测试一个需要认证的接口
func GetDataByTime(c *gin.Context) {
	claims := c.MustGet("claims").(*middleware.CustomClaims)
	if claims != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "token有效",
			"data":    claims,
		})
	}
}

func GetAllUser(c *gin.Context) {
	//allUserInfo := dao.SelectAllUserInfo()
	//allUserInfo := dao.SelectUserInfoById(1)
	//allUserInfo := dao.SelectAllRole()
	//for _, role := range *allUserInfo {
	//	role.ID = uint64(0)
	//}
	//dao.SaveRoleBatch(allUserInfo)
	//allUserInfo.ID = uint64(0)
	//allUserInfo.Cellphone = "123123231"
	//dao.DeleteUserInfoByExample(&dao.UserInfo{Gender: "2", UserType: "222"})
	//userInfo := dao.UserInfo{
	//	UserNo: "2",
	//	EnglishName: "admin",
	//	RealName: "admin",
	//	Gender: "1",
	//}
	//
	//dao.SaveUserInfo(&userInfo)

	wsserver.SendModelMessage("model", []byte("测试一下"))
	detail := dao.FindUserDetail(1)
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": detail,
	})
}
